<?php

namespace app\admin\model;

use think\Model;

class AuthRule extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * @param $rules 传入用户的权限生成用户的权限菜单
     * @return array 返回全数据菜单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getMenu($rules)
    {
        $menus = self::where('id', 'in', $rules)
            ->where(['ismenu'=>1,'status'=>1])
            ->field('id,pid,title name,icon,name as url')
            ->select();
        $menus = self::unlimitedSort($menus->toArray());
        return $menus;
    }

    public function treeList()
    {
        $list = $this->order('sorts ASC,id ASC')->select();
        $list = $this->GetTree($list);
        return $list;
    }

    public function treeList2($pid = 0)
    {
        $list = $this->field('id,pid,name,title,icon as lay_icon,status,ismenu,sorts')->order('sorts ASC,id ASC')->select();
        return $this->GetTree($list, $pid);
    }
    //带children格式
    /*title: '一级1'
                , id: 1
                , checked: true
                , spread: true
                , children: [{
                    title: '二级1-1 可允许跳转'
                    , id: 3
                    , spread: true
                    , children: [{
                        title: '三级1-1-3'
                        , id: 23
                        , children: [{
                            title: '四级1-1-3-1'
                            , id: 24
                            , children: [{
                                title: '五级1-1-3-1-1'
                                , id: 30
                            }, {
                                title: '五级1-1-3-1-2'
                                , id: 31
                            }]
                        }]
                    }*/
    //获取 layui tree添加角色是用到
    public function treeList3()
    {
        $list = $this->field('id,pid,title,name url')->order('sorts ASC,id ASC')->select();
        return $this->unlimitedSort($list);
    }

    public function getUserRule($rules)
    {
        $rules = explode(',', $rules);
        $list = $this->field('id,pid,title')->order('sorts ASC,id ASC')->select();
        return $this->unlimitedSortSel($list, $rules);
    }

    function GetTree($arr, $pid = 0, $step = 0)
    {
        global $tree;
        foreach ($arr as $key => $val) {
            if ($val['pid'] == $pid) {
                $val['level'] = $step + 1;
                $tree[] = $val;
                $this->GetTree($arr, $val['id'], $step + 1);
            }
        }
        return $tree;
    }

    function GetChildren($arr, $pid = 0, $level = 0)
    {
        global $list;
        foreach ($arr as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['spread'] = true;
                $list[] = $v;
                $v['children'] = $this->GetChildren($arr, $v['id'], $level + 1);
            }
        }
        return $list;
    }

    /**
     * 栏目无限级分类
     * @param $data array 待排序数组
     * @param $pid int  要查找的顶级分类
     * @param string $field 查找的字段
     * @param string $childNode 子节点名称
     * @return array
     */
    function unlimitedSortSel($data, $rules, $pid = 0, $field = 'pid', $childNode = 'children')
    {
        $tree = [];
        foreach ($data as $item) {
            if ($item[$field] == $pid) {
                $item[$childNode] = $this->unlimitedSortSel($data,$rules, $item['id'], $field);
                if (in_array($item['id'], $rules)) {
                    $item['checked'] = true;
                }
                // 卸载掉空的数组元素
                if ($item[$childNode] == null) {
//                    unset($item[$childNode]);
                } else {
                    unset($item['checked']);
                    $item['spread'] = true;
                }
                $tree[] = $item;
            }
        }
        return $tree;
    }

    /**
     * 栏目无限级分类
     * @param $data array 待排序数组
     * @param $pid int  要查找的顶级分类
     * @param string $field 查找的字段
     * @param string $childNode 子节点名称
     * @return array
     */
    public static function unlimitedSort($data, $pid = 0, $field = 'pid', $childNode = 'children')
    {
        $tree = [];
        foreach ($data as $item) {
            if ($item[$field] == $pid) {
                $item[$childNode] = self::unlimitedSort($data, $item['id'], $field);
                $item['url']=url($item['url']);//替换链接。驼峰命名转下划线
                if (!$item[$childNode] == null) {
                    $item['spread'] = true;
                }
                $tree[] = $item;
            }
        }
        return $tree;
    }

}